5841. Find the Longest Valid Obstacle Course at Each Position
5841. Find the Longest Valid Obstacle Course at Each Position
tag: monotone-stack, greedy, Maximum Increasing Subsequence
Description
Solution
Use monotone-stack to find the longest subsequence end with obstacles[i]
Greedily replace the very obstacles[j], j < i
that exactly greater than obstacles[i]
, other elements in the stack just remain
1 | 1 3 5 7 4 |
Just be careful about the same number should also be inclueded, so just binary search for (obstacle[i] + 1)
Code
1 | class Solution { |
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.